package com.hae.shop.micoshopbasicszuul;

import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
//@EnableApolloConfig
public class MicoShopBasicsZuulApplication {

//     @ApolloConfig
//     private Config config;

    public static void main(String[] args) {
        SpringApplication.run(MicoShopBasicsZuulApplication.class, args);
    }

    @RefreshScope
    @ConfigurationProperties("zuul")
    public ZuulProperties zuulProperties() {
        return new ZuulProperties();
    }

    // 添加文档来源
    @Component
    @Primary
    class DocumentationConfig implements SwaggerResourcesProvider {
        @Override
        public List<SwaggerResource> get() {
            List resources = new ArrayList<>();
            // 方式一：从配置文件读取，写死。 访问页面http://localhost:8020/swagger-ui.html#/swagger-controller，name即是swagger页面显示的名称。
//            resources.add(swaggerResource("test-wechat", "/api-wechat/v2/api-docs", "2.0"));
            resources.add(swaggerResource("test-member", "/api-member/v2/api-docs", "2.0"));
//            resources.add(swaggerResource("test-product", "/api-product/v2/api-docs", "2.0"));
            resources.add(swaggerResource("test-pay", "/api-pay/v2/api-docs", "2.0"));
            resources.add(swaggerResource("test-order", "/api-order/v2/api-docs", "2.0"));
            //方式二：使用apoll动态获取
//            String swaDocJson = config.getProperty("hae-mail.zuul.swagger.document", null); //需注明在apllo里的配置名称
//            JSONArray docJsonArray = JSONArray.parseArray(swaDocJson);
//            // 遍历集合数组
//            for (Object object : docJsonArray) {
//                JSONObject jsonObject = (JSONObject) object;
//                String name = jsonObject.getString("name");
//                String location = jsonObject.getString("location");
//                String version = jsonObject.getString("version");
//                resources.add(swaggerResource(name, location, version));
//            }
            return resources;
        }

        private SwaggerResource swaggerResource(String name, String location, String version) {
            SwaggerResource swaggerResource = new SwaggerResource();
            swaggerResource.setName(name);
            swaggerResource.setLocation(location);
            swaggerResource.setSwaggerVersion(version);
            return swaggerResource;
        }
    }

}
